package org.example.io;

import io.netty.util.concurrent.DefaultThreadFactory;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(20, 30, 10, TimeUnit.MINUTES, new SynchronousQueue<>(), new DefaultThreadFactory("abc"));

    public static void main(String[] args) throws Exception {
        CountDownLatch countDownLatch = new CountDownLatch(20);
        for (int i = 0; i < 20; i++) {
            threadPool.execute(() -> {
                while (true) {
                    try {
                        URL url = new URL("https://www.baidu.com");
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        urlConnection.connect();
                        InputStream inputStream = urlConnection.getInputStream();
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        while ((len = inputStream.read(bytes)) != -1) {
                            String str = new String(bytes, 0, len);
                            System.out.println(str);
                        }
                        System.out.println(url.getPort());
                        System.out.println(url.getProtocol());
                        inputStream.close();
                        urlConnection.disconnect();
                    } catch (Exception ignored) {

                    }

                }
            });
        }
        countDownLatch.await();
    }
}
